package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.dto.UserRequestDTO;
import com.example.demo.controller.bean.Result;
import com.example.demo.mapper.UserManageMapper;
import com.example.demo.mapper.entity.*;
import com.example.demo.service.UserManageService;
import com.example.demo.utils.MD5Util;
import com.example.demo.utils.PageBo;
import com.example.demo.utils.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserManageServiceImpl extends ServiceImpl<UserManageMapper, SysUser> implements UserManageService {
    @Autowired
    private UserManageMapper mapper;

    @Override
    public PageVo<SysUserView> getUserList(PageBo pageBo) {
        List<SysUserView> logs = mapper.getUserList();
        return new PageVo<>(pageBo, logs);
    }

    @Override
    public PageVo<SysUserView> searchUser(UserRequestDTO request, PageBo pageBo) {
        String number = request.getNumber();
        String name = request.getName();

        List<SysUserView> list = mapper.getUserList();
        //按照編號，姓名搜索
        list = list.stream()
                .filter(o -> (number == null || o.getNumber().contains(number))&&
                        (name == null || o.getName().contains(name))).collect(Collectors.toList());
        return new PageVo<>(pageBo, list);
    }

    @Override
    public Result<String> deleteUser(SysUserView user) {
        int userId = user.getUserId();
        if(userId!=0){
            //删除用户表的关联表
            //2.用户角色表sys_user_role
            mapper.deleteSysRole(userId);
            //1.用户信息表sys_user
            mapper.deleteSysUser(userId);
;
            return Result.ok("删除成功");
        }else{
            return Result.error(400, "删除失败");
        }
    }

    @Override
    public void deleteList(List<SysUserView> userList) {
        // 遍历 userList 并输出每个元素
        for (SysUserView user : userList) {
            System.out.println(user);
        }
        mapper.deleteRoleList(userList);
        mapper.deleteUserList(userList);
    }

    @Override
    public Result<String> updateUser(SysUserView user) {
        SysUser curUser=new SysUser();
        curUser.setUserId(user.getUserId());
        curUser.setNumber(user.getNumber());
        curUser.setName(user.getName());
        //更新用戶表内容
        if(curUser!=null) mapper.updateUserById(curUser);

        //更新角色表
        SysRole userRole = mapper.getRoleByUserId(user.getUserId());
        //如果类型为目录，设置父菜单为0
        if (user.getRoleId()==(userRole.getRoleId())){
            return Result.warn("用户角色修改前后一致");
        }else{
            if(user.getRoleId()!=0)  userRole.setRoleId(user.getRoleId());
            mapper.updateUserRoleId(user);
        }
        return Result.ok("修改成功");
    }

    @Override
    public PageVo<AdvertiserView> getAdvertiserList(PageBo pageBo) {
        List<AdvertiserView> logs = mapper.getAdvertiserList();
        return new PageVo<>(pageBo, logs);
    }

    @Override
    public PageVo<AdvertiserView> searchAdvertiser(UserRequestDTO request, PageBo pageBo) {
        String number = request.getNumber();
        String name = request.getName();

        List<AdvertiserView> list = mapper.getAdvertiserList();
        //按照編號，姓名搜索
        list = list.stream()
                .filter(o -> (number == null || o.getNumber().contains(number))&&
                        (name == null || o.getName().contains(name))).collect(Collectors.toList());
        return new PageVo<>(pageBo, list);
    }

    @Override
    public List<AdvertiserView> getAdvertiserDataList(int userId) {
        return mapper.getAdvertiserDataList(userId);
    }

    @Override
    public Result<String> addAdvertiser(AdvertiserView request) {
        SysUser newUser=new SysUser();
        newUser.setName(request.getName());
        newUser.setNumber(request.getNumber());
        newUser.setEmail(request.getEmail());
        newUser.setAvatar(request.getAvatar());
        newUser.setPhone(request.getPhone());
        newUser.setPassword(MD5Util.encryptPassword("abc123"));
        newUser.setState(2);

        mapper.insertUser(newUser);
        int userId = newUser.getUserId();
        if(userId!=0){
            request.setUserId(userId);
            SysUserRole newUserRole=new SysUserRole();
            newUserRole.setUserId(userId);
            newUserRole.setRoleId(4);
            int sysUserRole=mapper.insertUserRole(newUserRole);
            int advertiser = mapper.insertAdvertiser(request);
            if(sysUserRole!=0&&advertiser!=0){
                return Result.ok("添加成功");
            }
        }
        return Result.error(400, "添加失败");
    }

    @Override
    public Result<String> deleteAdvertiser(AdvertiserView request) {
        int deleteRole=mapper.deleteSysRole(request.getUserId());
        int deleteAdvertiser=mapper.deleteAdvertiserList(request);

        if(deleteRole!=0&&deleteAdvertiser!=0){
            int deleteSysUser=mapper.deleteSysUser(request.getUserId());
            if(deleteSysUser!=0){
                return Result.ok("删除成功");
            }
            return Result.error(400, "删除失败");
        }
        return Result.error(400, "删除失败");
    }

    @Override
    public Result<String> batchDeleteAdvertiser(List<AdvertiserView> advertiserList) {
        int deleteRole=mapper.batchDeleteSysRole(advertiserList);
        int deleteAdvertiser=mapper.batchDeleteAdvertiserList(advertiserList);
        if(deleteRole!=0&&deleteAdvertiser!=0){
            int deleteSysUser=mapper.batchDeleteSysUser(advertiserList);
            if(deleteSysUser!=0){
                return Result.ok("删除成功");
            }
            return Result.error(400, "删除失败");
        }
        return Result.error(400, "删除失败");
    }

    @Override
    public Result<String> updateAdvertiser(AdvertiserView request) {
        if(request!=null) {
            int updateAdvertiser=mapper.updateAdvertiser(request);
            if(updateAdvertiser!=0){
                int updateSysUser=mapper.updateAdvertiserUserById(request);
                if(updateSysUser!=0){
                    return Result.ok("修改成功");
                }
                return Result.error(400, "修改失败");
            }
            return Result.error(400, "修改失败");
        }
        return Result.error(400, "修改失败");

    }

    @Override
    public Result<String> addUserInfo(SysUserView user) {
        SysUser newUser=new SysUser();
        newUser.setName(user.getName());
        newUser.setNumber(user.getNumber());
        newUser.setPassword(MD5Util.encryptPassword("abc123"));
        newUser.setState(2);
        mapper.insertUser(newUser);

        SysUserRole userRole=new SysUserRole();
        userRole.setUserId(newUser.getUserId());
        userRole.setRoleId(user.getRoleId());
        if(mapper.insertUserRole(userRole)>0){
            return Result.ok("添加成功");
        }
        return Result.error(400, "添加失败");

    }


}
